<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Index_Test extends Controller_Index_Base
{
    public function before()
    {
       // die('dsds');
    }
    
    /*public function action_index()
    {
        if (HTTP_Request::POST == $this->request->method()) {
            $email    = $this->request->post('email');
            //$password = $this->request->post('password');
            $url      = $this->request->post('url');
                  
            try {
                $password = ORM::factory('user')->generate_password();
                
                $user = ORM::factory('user')->add_user($email, $password);
                $shop = ORM::factory('shop')->add_shop($url);                                
                $_POST = array();
                 
                $user->add_to_shop($shop); 
                Model_Auth::instance()->login($email, $password, true);
                  
                $shop->set_as_current();                     
                Request::current()->redirect(Helper_Admin::get_url() . '/index/start'); 
            } catch (Model_Exception $e) {
                $message = 'Форма заполнена с ошибками!';
            }
        }
        
        //Model_Auth::instance();
                
        $content = new View('index/index/index_tpl');        
        $this->print_template('', $content); 
    }*/
    
    public function action_test()
    {
        die();
        set_time_limit(0);
        $shop_id = $this->request->param('id') ? $this->request->param('id') : null;
        if (!$shop_id) {
            die();    
        }
        $shop = ORM::factory('shop', $shop_id);
        $shop->set_as_current();
        Model_ProductParser_YML::instance()->parse($shop);
    }
    
    public function action_test1()
    {
        die();
        /*$shop = ORM::factory('shop', 26);
        $shop->set_as_current();
        Model_ProductParser_YML::instance()->parse($shop);*/   
        
        set_time_limit(0);
        $shop = ORM::factory('shop', 27);
        $shop->set_as_current();
        ORM::factory('model')->process_shop($shop);  
        
        /*$model = ORM::factory('model', 27);
        Model_ReviewParser_YandexMarket::instance()->parse($model);*/ 
        
        /*$yes = 132;
        $no = 1111;
        echo round(((($yes + 1.9208) / ($yes + $no) - 
            1.96 * sqrt(($yes * $no) / ($yes + $no) + 0.9604) / 
            ($yes + $no)) / (1 + 3.8416 / ($yes + $no))) * 10000); */
            
        //echo Model_Auth::instance()->hash_password('123456');   
    }
    
    public function action_test2()
    {
        die();
        set_time_limit(0);
        $shop = ORM::factory('shop', 27);
        $shop->set_as_current();
        ORM::factory('model')->process_shop2($shop); 
    }
    
	/*public function action_index1()
	{    
        $product = ORM::factory('product')->find(123);        
        

        $category = ORM::factory('category', 3);
        //$category = new Model_Category(1);
        //$category = ORM::factory('category')->find(1);   
        
        //$categories = ORM::factory('category')->get_childs();    
        $categories = $category->get_childs();
        $breadcrumbs = $category->get_breadcrumbs(); 
        
        $products = ORM::factory('product')->get_by_category($category);
        
        
        foreach($breadcrumbs as $category) {
            echo $category->name.' -> ';
        }
        echo '<br />';
        
        foreach($categories as $category) {
            echo $category->name.' - '.$category->get_parent()->id.'<br />';
        }
        
        foreach($products as $product) {
            echo $product->name.'  <img src="'.$product->picture.'" /><br />';   
        }
        
        die();
        		
        //$this->print_template($title, $content, $menu);
	} */
    
    public function action_test3() {
        die();
        set_time_limit(0);
        
        $models = ORM::factory('model')
                //->where('count_reviews', '=', 0)
                ->offset(40000)
                ->limit(20000)
                ->find_all();
                
        foreach ($models as $model) {
            //$reviews = ORM::factory('review')->get_reviews($model);
            /*foreach ($reviews as $review) {
                $review->usefulness = $review->get_usefulness();
                $review->save();
                //$model->change_count_stars($review->rating);
            }       */
            /*foreach ($reviews as $review) {
                $model->change_count_stars($review['rating']);
            }
            if (count($reviews)) {
                $model->count_reviews = count($reviews);
                $model->save();
            }   */
            $model->reviews_score = $model->get_reviews_score();
            $model->save(); 
        }
        echo 'ok';
    }     
    
    /*public function action_test3()
    {
        die();
        //echo Model_Auth::instance()->hash_password('123456');
        
        //ORM::factory('user')->add_user('akazorin@gmail.com', 'abc26101991xyz'); 
        
        /*$user = ORM::factory('user', 1);
        $shop = ORM::factory('shop', 3);
        $user->add_to_shop($shop);*/
        
        //var_dump(Model_Auth::instance()->login('akazorin@gmail.com', 'abc26101991xyz', true));
        //Model_Auth::instance()->logout();
        //Model_Auth::instance()->force_login('akazorin@gmail.com');
        //var_dump(Model_Auth::instance()->get_user()->in_shop(ORM::factory('shop', 3))); 
        //var_dump(Model_Auth::instance()->auto_login());
        //var_dump(Model_Auth::instance()->check_password('abc26101991xyz'));
        //var_dump(Model_Auth::instance()->logged_in(Model_User_Shop::ROLE_ADMINISTRATOR));
        
        /*DB::query(NULL, 'CREATE TABLE `ugol`.`categories_2` (
                            `id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
                            `name` VARCHAR( 255 ) NOT NULL ,
                            `parent_id` SMALLINT UNSIGNED NOT NULL DEFAULT \'0\',
                         INDEX ( `parent_id` )
                         ) ENGINE = MYISAM'
        )->execute();   
        
        
    }   */
    
    public function action_test4()
    {
		die();
        set_time_limit(0);
        $models = ORM::factory('model')
            ->offset(6700)
            ->limit(500)
            ->find_all(); 
        foreach ($models as $model) {
            echo '#' . $model->id . ' - ' . $model->name . ':<br />';
            $urls = Model_ReviewParser_Rozetka::instance()->get_reviews_urls($model);
            if (count($urls)) {
                $model->set_rozetka_urls($urls);
                echo '<br /><br />';      
            }  
        }  
    }
    
   /* public function action_test5()
    {
        set_time_limit(0);
        //$model = ORM::factory('model', 2699);
        //Model_ReviewParser_Rozetka::instance()->parse($model); 
        $models = ORM::factory('model')
            ->offset(7200)
            ->limit(300)
            ->find_all(); 
        foreach ($models as $model) {
            Model_ReviewParser_Rozetka::instance()->parse($model);
        }
    }       */
    
    public function action_test6()
    {
		die();
		set_time_limit(0);
        $models = ORM::factory('model')
            ->where('market_url', '!=', NULL)
            ->and_where('market_url', '!=', '#')
            ->offset(6900)
            ->limit(300)
            ->find_all();
        foreach ($models as $model) {
            $id = (int)str_replace('http://market.yandex.ru/model-opinions.xml?modelid=', '', $model->market_url);           
            $url = 'http://market.yandex.ru/model.xml?modelid='.$id;
            
            $source = Model_ReviewParser_Abstract::_get_source_code($url);
            $source = Model_ReviewParser_Abstract::_cut_str('<span class="b-model-pictures__big"', '</span>', $source);
			/*$img = Model_ReviewParser_Abstract::_cut_str(' href="', '"', $source);
			if (!$img) {
				$img = Model_ReviewParser_Abstract::_cut_str(' src="', '"', $source);
			}*/
			$img = Model_ReviewParser_Abstract::_cut_str(' src="', '"', $source);
			if (!$img) {
				continue;
			}
			
			$dir = APPPATH.'../media/uploads/models/';
			$dir = $dir.sprintf('%02x', $model->id % 0xff);
			if (!is_dir($dir)) {
				mkdir($dir);
			}
			$dir = $dir.sprintf('/%d', $model->id);
			if (!is_dir($dir)) {
				mkdir($dir);
			}
			
			$data = file_get_contents($img);
			$f = fopen($dir.'/main.jpg', 'w+');
			fputs($f, $data);
			fclose($f);
			
            echo $model->market_url.' '.$id.' '.$img.'<br />';   
        }
                                               
    }
    
    public function action_test5()
    {
        die();
        set_time_limit(0);
        $tmp_models = ORM::factory('model')
            ->order_by('id', 'DESC')  
            ->offset(60000)
            ->limit(10000)
            ->find_all();
        $models = array();
        foreach ($tmp_models as $model) {
            $models[$model->id] = $model;    
        }
        foreach ($models as &$model) {
            $doubles = ORM::factory('model')
                        ->where('name', 'LIKE', $model->name)
                        ->and_where('id', '!=', $model->id)
                        ->find_all();
            echo $model->id.' - '.$model->name.':<br />';
            foreach ($doubles as $double) {
                echo '<span style="color: red;">' . $double->name . '</span><br />';
                
                /*DB::delete('reviews')
                    ->where('model_id', '=', $double->id)
                    ->execute();  */
                  
                DB::update('products')
                    ->set(array('model_id' => $model->id))
                    ->where('model_id', '=', $double->id)
                    ->execute();    
                    
                unset($models[$double->id]);
                $double->delete();   
            } 
        }
    }   
    
    public function action_test7()
    {
        die();
        set_time_limit(0);
        $models = ORM::factory('model')
            ->offset(7200)
            ->limit(300)
            ->find_all(); 
        foreach ($models as $model) {
            echo '#' . $model->id . ' - ' . $model->name . ':<br />';
            $urls = Model_ReviewParser_Otzovik::instance()->get_reviews_urls($model);
            if (count($urls)) {
                $model->set_otzovik_urls($urls);
                echo '<br /><br />';      
            }  
        }  
    }
    
    public function action_test8()
    {
        die();
        set_time_limit(0);
        //$model = ORM::factory('model', 2707);
        $model = ORM::factory('model', 580);  
        Model_ReviewParser_Otzovik::instance()->parse($model); 
        /*$models = ORM::factory('model')
            ->offset(7200)
            ->limit(300)
            ->find_all(); 
        foreach ($models as $model) {
            Model_ReviewParser_Rozetka::instance()->parse($model);
        }  */
    }
    
    // Модели с м
    public function action_test9()
    {  
        die();
        set_time_limit(0);     
        $category_id  = 222;
        $category_url = 'http://market.yandex.ru/guru.xml?CMD=-RR=0,0,0,0-VIS=70-CAT_ID=1620138-EXC=1-PG=10&hid=91610';
		
        function get_next_page_url($data) {
            if ($url = Model_ReviewParser_Abstract::_cut_str('class="b-pager__next" href="', '"', $data)) {
                return 'http://market.yandex.ru'.$url;
            } 
            return null;     
        }
        
        function get_model_data(&$data) {
            if ($_data = Model_ReviewParser_Abstract::_cut_str('class="b-offers b-offers_type_guru"', 'class="b-model-actioins__shoplist g-none"', $data)) {
                return $_data;
            } 
            return null;
        } 
        
        function get_model_link($data) {
            if ($link = Model_ReviewParser_Abstract::_cut_str('class="b-offers__name" href="', '&amp;', $data)) {
                return 'http://market.yandex.ru'.$link;
            } 
			if ($link = Model_ReviewParser_Abstract::_cut_str('href="/model.xml?', '"', $data)) {
                return 'http://market.yandex.ru/model.xml?'.$link;
            } 
            return null;   
        }
        
        function get_model_name($data) {
            if ($name = Model_ReviewParser_Abstract::_cut_str('<h3 class="b-offers__title">', '</a>', $data)) {
                return strip_tags($name);
            } 
            return null;   
        }
        
        function get_model_image($data) {
            if ($image = Model_ReviewParser_Abstract::_cut_str('class="b-offers__img" src="', '"', $data)) {
                return $image;
            }
			if ($image = Model_ReviewParser_Abstract::_cut_str('src="http://mdata.yandex.net/i', '"', $data)) {
                return 'http://mdata.yandex.net/i'.$image;
            }			
            return null;   
        }
        
        function get_model_count_prices($data) {
            if ($count = Model_ReviewParser_Abstract::_cut_str('Цены</a>', '</li', $data)) {
                if ($count = Model_ReviewParser_Abstract::_cut_str('class="b-model-actioins__cnt">', '</span', $count)) {
                    return (int)$count;
                } 
            } 
            return 0;   
        }
        
        function get_model_count_reviews($data) {
            if ($count = Model_ReviewParser_Abstract::_cut_str('Отзывы</a>', '</li', $data)) {
                if ($count = Model_ReviewParser_Abstract::_cut_str('class="b-model-actioins__cnt">', '</span', $count)) {
                    return (int)$count;
                } 
            } 
            return 0;   
        }

        $i = 0;
        do {
            $data = Model_ReviewParser_Abstract::_get_source_code($category_url);
            while ($model_data = get_model_data($data)) {
                $name           = get_model_name($model_data);
                $link           = get_model_link($model_data);   
                $image          = get_model_image($model_data);   
                $count_prices   = get_model_count_prices($model_data);   
                $count_reviews  = get_model_count_reviews($model_data);
				DB::insert('tmp_models', array('category_id', 'name', 'link', 'image', 'count_prices', 'count_reviews'))
                    ->values(array($category_id, $name, $link, $image, $count_prices, $count_reviews))->execute();			  
                //echo  $name . ' - ' . $link . ' - ' . $image . ' - ' . $count_prices . ' - ' . $count_reviews . '<br />';
                    
            }
			echo $category_url . '<br />';

			usleep(700000);
            //sleep(1);

            /*$i++;
            if ($i > 5) die();   */
            
        } while ($category_url = get_next_page_url($data));
        
    }
    
    public function action_test10()
    {
        die();
        set_time_limit(0);
        $file = fopen('models123.txt', 'w+');
        $i = 0;
        while (1) {
            $models = DB::select()->from('models')->limit(1000)->offset($i * 1000)->execute()->as_array(); 
            if (!count($models)) {
                break;
            } 
            foreach ($models as $model) {
                fputs($file, $model['id'] . '---' . $model['name'] . "\r\n"); 
            }  
            $i++;
        }
        fclose($file); 
    }
    
    public function action_test11()
    {
        die();
        set_time_limit(0);
        $i = 144;
        while (1) {
            $models = DB::select()->from('tmp_models')->limit(1000)->offset($i * 1000)->execute()->as_array(); 
            if (!count($models)) {
                break;
            } 
            foreach ($models as $model) {
                /*//$name = iconv('utf-8', 'cp1251', $model['name']);
                $name =  $model['goodname'];
                $words = preg_split('/\s/i', $name);
                $name = implode(' ', $words);
                //$name = iconv('cp1251', 'utf-8', $name);  
                echo $name.'<br />';      
                //DB::update('tmp_models')->value('goodname', $name)->where('id', '=', $model['id'])->execute();*/
                
                
                $tmp_name = mb_strtolower(str_replace(array(' ', '-', '(', ')', '.', ',', '+', '/', '&amp;'), '', $model['goodname']));
                //echo $tmp_name.'<br />';
                DB::update('tmp_models')->value('tmp_name', $tmp_name)->where('id', '=', $model['id'])->execute();				
            }  
            $i++;
        }    
    }
    
    public function action_test12()
    {
        die();
        set_time_limit(0); 
        $file = fopen('models123.txt', 'r+');
		$i = 0;
        while ($str = fgets($file)) {
			$i++;
            list($id, $name) = explode('---', $str);
			//if ($id > 154921) {
				//DB::update('tmp_models')->value('goodname', $name)->where('id', '=', $id)->execute();
                DB::update('models')->value('name', trim($name))->where('id', '=', $id)->execute(); 
			//}
        }  
		echo $i;		
    }
	
	public function action_test13()
	{
        die();
		set_time_limit(0); 
        $file = fopen('models_searches.txt', 'r+');
		$i = $j = 0;
        while ($str = fgets($file)) {
			$i++;
            list($name, $searches) = explode("\t", $str);
			$name = str_replace(array(' ', '-', '(', ')', '.', ',', '+', '/', '&amp;'), '', $name);
			$models = DB::select('id', 'tmp_name')->from('tmp_models')->where('tmp_name', 'LIKE', '%'.$name.'%')->and_where('count_searches', 'IS', NULL)->execute()->as_array();
			if (count($models) == 0) {
				$j++;
				continue;
			}
			$model = $models[0];
			DB::update('tmp_models')->value('count_searches', $searches)->where('id', '=', $model['id'])->execute();
        }  
		echo $i . ' - ' . $j;
	}
	
	public function action_test14()
	{
        die();
		set_time_limit(0);
        $i = 144;
        while (1) {
            $models = DB::select()->from('tmp_models')->limit(1000)->offset($i * 1000)->execute()->as_array(); 
            if (!count($models)) {
                break;
            } 
            foreach ($models as $model) {
				$_link = $model['link'].'&';
				if (!($modelid = Model_ReviewParser_Abstract::_cut_str('modelid=', '&', $_link))) {
					die($model['id']);
				}
				$link = 'http://market.yandex.ru/model-opinions.xml?modelid='.$modelid;
                
                DB::update('tmp_models')->value('reviews_link', $link)->where('id', '=', $model['id'])->execute();				
            }  
            $i++;
        }    
	}
	
	public function action_test15()
	{
        die();
		set_time_limit(0);
        $i = 0;
        while (1) {
            $models = DB::select()->from('tmp_models')->where('count_reviews', '>', 0)->or_where('count_searches', '>', 0)->limit(1000)->offset($i * 1000)->execute()->as_array(); 
            if (!count($models)) {
                break;
            } 
            foreach ($models as $model) {
				$items = DB::select()->from('models')->where('market_url', '=', $model['reviews_link'])->execute()->as_array();
				if (count($items) == 1) {
					DB::update('models')->value('potential_count_reviews', $model['count_reviews'])
									    ->value('count_searches', $model['count_searches'])->where('id', '=', $items[0]['id'])->execute();	
				} else {
					try { 
						DB::insert('models', array('name', 'potential_count_reviews', 'count_searches', 'market_url'))
							->values(array($model['name'], $model['count_reviews'], $model['count_searches'], $model['reviews_link']))->execute();
					} catch (Database_Exception $e) {
						if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
							continue;
						}
						die($e->getMessage());
					}
				}			
            }  
            $i++;
        }    
	}
	
	public function action_test16()
	{
		die();
        set_time_limit(0);
        $models = ORM::factory('model')
			->where('id', '>', 28001)
			->and_where('id', '<', 61666)
			->and_where('potential_count_reviews', '>', 0)
            ->offset(0)
            ->limit(500)
            ->find_all(); 
        foreach ($models as $model) {
            Model_ReviewParser_YandexMarket::instance()->parse($model);
        }
	}
	
	public function action_test17()
	{
		die();
		$ids = DB::query(Database::SELECT, 'SELECT t1.id FROM yandexmarket_reviews AS t1
							   JOIN yandexmarket_reviews AS t2 ON (t1.model_id = t2.model_id AND t1.source_id = t2.source_id AND t1.id < t2.id)'
        )->execute()->as_array();
		foreach ($ids as $id) {
			DB::delete('yandexmarket_reviews')
                    ->where('id', '=', $id)
                    ->execute();
		}
	}
	
	public function action_test18() {
		die();
		set_time_limit(0);
        $models = ORM::factory('model')
				->offset(60000)
				->limit(5000)
				->find_all();	// where('count_reviews', '=', 0)
		//echo count($models);
		//die();
        foreach ($models as $model) {
            $reviews = DB::select()->from('yandexmarket_reviews')->where('model_id', '=', $model->id)->execute()->as_array();;
            foreach ($reviews as $review) {
                $model->change_count_stars($review['rating']);
            }
			if (count($reviews)) {
				$model->count_reviews = count($reviews);
				$model->save();
			}
        }
		echo '312312';
    }
    
    public function action_test19()
    {
        set_time_limit(0);
        $param = $this->request->param('id') ? $this->request->param('id') : null;
        if (!$param) {
            die();    
        }
        list($shop_id, $category_id) = explode('-', $param);
        if (!$shop_id) {
            die();    
        }
        if (!$category_id) {
            $category_id = null;
        }
        $shop = ORM::factory('shop', $shop_id);
        $shop->set_as_current();
        
        $products = ORM::factory('product')->get_products($category_id, 0); 
        $count = $count_found = 0;
        foreach ($products as $product) {
            $count++;
            echo $count.' ('.$product->id.') - '.$product->name;
            
            $models = ORM::factory('model')->search($product->clean_name, 5);
            if ($models) {
                foreach ($models as $model) {
                    //echo '<br />'.$product->get_clean_name().' - '.$model->get_clean_name();
                    echo '<br /><span style="color:green;">'.$model->name.'</span> ('.$model->count_reviews.') <a href="/index/test/to_model/'.$product->id.'-'.$model->id.'" target="_blank">привязать</a>';
                    if ((strstr($product->get_clean_name(), $model->get_clean_name()) !== false ||
                            strstr($model->get_clean_name(), $product->get_clean_name()) !== false)) {
                        $product->to_model($model);
                        $count_found++;
                        echo '<br /><span style="color:red;">В '.$model->name.'</span><br />';
                        break;        
                    }    
                }
                echo '<br /><br />'; 
            }
        }
        echo '<br /><br />Всего - '.count($products).'<br />'.
            'Нашлось наших моделей - '.$count_found.'<br />';
    }
    
    public function action_to_model()
    {
        $param = $this->request->param('id') ? $this->request->param('id') : null;
        if ($param) {
            list($product_id, $model_id) = explode('-', $param);
            $product = ORM::factory('product', $product_id);
            $model   = ORM::factory('model', $model_id);
            $product->to_model($model);
        }
    }
    
    // Модели с отзовика
    public function action_test20()
    {  
        set_time_limit(0);     
        $category_id  = 2;
        $category_url = 'http://www.otzovik.com/auto/autospares/tires/';
        
        function get_next_page_url($data) {
            if ($url = Model_ReviewParser_Abstract::_cut_str('<a class=\'npage\' title=\'Следующая\' href=\'', '\'', $data)) {
                return 'http://www.otzovik.com'.$url;
            } 
            return null;     
        }
        
        function get_model_data(&$data) {
            if ($_data = Model_ReviewParser_Abstract::_cut_str("<td style='padding: 5px' class=imagesel", "</tr>", $data)) {
                return $_data;
            } 
            return null; 
        } 
        
        function get_model_link($data) {
            if ($link = Model_ReviewParser_Abstract::_cut_str("<a href=", ">", $data)) {
                return 'http://www.otzovik.com'.$link;
            }  
            return null;   
        }
        
        function get_model_name($data) {
            if ($name = Model_ReviewParser_Abstract::_cut_str("<td class=nprods>", "</td>", $data)) {
                return strip_tags($name);
            } 
            return null;   
        }
        
        function get_model_image($data) {
            if ($image = Model_ReviewParser_Abstract::_cut_str(" src=", " width", $data)) {
                if ($image != 'http://i.otzovik.com/nophoto_m.png') {
                    return $image;      
                }
            }           
            return null;   
        }
        
        function get_model_count_reviews($data) {
            if ($count = Model_ReviewParser_Abstract::_cut_str(">Отзывы:", "</a>", $data)) {
                return (int)trim($count);
            } 
            return 0;   
        }

        $i = 0;
        do {
            $data = Model_ReviewParser_Abstract::_get_source_code($category_url);
            while ($model_data = get_model_data($data)) {
                $i++; 
                $name           = get_model_name($model_data);
                $link           = get_model_link($model_data);   
                $image          = get_model_image($model_data);    
                $count_reviews  = get_model_count_reviews($model_data);
                //DB::insert('tmp_models', array('category_id', 'name', 'link', 'image', 'count_reviews'))
                //    ->values(array($category_id, $name, $link, $image, $count_reviews))->execute();              
                echo  $i . ' - ' . $name . ' - ' . $link . ' - ' . $image . ' - ' . $count_reviews . '<br />';
                    
            }
            echo $category_url . '<br />';

            usleep(700000);
            //sleep(1);
            
        } while ($category_url = get_next_page_url($data));
        
    }
    
    public function action_test23()
    {
        //die();
        set_time_limit(0); 
        $categories = array(1936, 1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967,);
        $products = DB::select()
                ->from('tmp_models_3')
                ->where('category_id', 'IN', $categories)
                ->and_where('model_id', 'IS', NULL)
                ->execute()
                ->as_array();
        foreach ($products as $product) {
			try {
				$model = DB::insert('models', array('name', 'tmp_category_id_3', 'potential_count_reviews_3', 'irecommend_url'))
						->values(array($product['name'], $product['category_id'], $product['count_reviews'], $product['link']))->execute();
				DB::update('tmp_models_3')->value('model_id', $model[0])->where('id', '=', $product['id'])->execute();        
			} catch (Database_Exception $e) {
				if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
					continue;
				}
				die($e->getMessage());
			}
        }    
        echo count($products); 
    } 
    
    public function action_test21()
    {	
		//die();
        set_time_limit(0); 
        $categories = array(1936, 1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967,); 
        $products = DB::select()
                ->from('tmp_models_3')
                ->where('category_id', 'IN', $categories)
                //->and_where('model_id', 'IS', NULL)
                //->offset(0)
                //->limit(1000)
                ->execute()
                ->as_array(); 
        $count_found = $count = 0;
		
		function _get_clean_name($name) {
			return $name;
			// TODO Если есть слово "для", то оставляем все как есть (???)
			
			$name = _cut_trash($name);
			$words = _get_words($name);
			
			$morphy = Morphy::instance();
			$result = array();
			foreach ($words as $word) {
				$word = mb_strtoupper(trim($word));
				if (!$word) {
					continue;
				}
				
				// Если не нашли, значит либо название товара, либо еще что-то
				// Сохраняем, а русские слова удаляем
				if (!($part_of_speech = $morphy->getPartOfSpeech($word))) {
					$result[] = $word;         
				}   
			}
			
			$result = implode(' ', $result);
			if (!trim($result)) {
				return 'Dfsdfsdw43 324 342';
			}
			return $result;
		}
		
		function _cut_trash($name) {
			// Вырезаем содержимое скобок - (серый)
			// Чаще всего там мусор или цвет
			$name = _cut_brackets($name); 
			
			// TODO Вырезать " и &quot (???)
			//$name = $this->_cut_quotes($name); 
			
			// TODO Удалять все, что справа "+"      
			
			return trim($name);
		}
		
		function _cut_brackets($name, $cut_single_bracket = true) {
			$result = preg_replace('/(\([^\)]+\))/i', '', $name);
			if ($cut_single_bracket) {
				$result = preg_replace('/(\([^\)]+)/i', '', $result);    
			}
			return $result;   
		}
		
		function _get_words($name) {
			$words = explode(' ', $name);
			
			// Удаляем запятые
			foreach ($words as &$word) {
				$word = str_replace(',', '', $word);    
			}
			
			return $words;
		}
		
        foreach ($products as $product) {
            $count++;
            echo $count.' ('.$product['id'].') - '.$product['name'];
            
            $found = false;
            $models = ORM::factory('model')->search($product['name'], 15);
            if ($models) {
                foreach ($models as $model) {
                    //echo '<br />'.$product->get_clean_name().' - '.$model->get_clean_name();
                    echo '<br /><span style="color:green;">'.$model->name.'</span> ('.$model->count_reviews.') <a href="/index/test/test22/'.$product['id'].'-'.$model->id.'" target="_blank">привязать</a>';
					if ((strstr(Model_Main::get_clean_name(_get_clean_name($product['name'])), $model->get_clean_name()) !== false ||
                            strstr($model->get_clean_name(), Model_Main::get_clean_name(_get_clean_name($product['name']))) !== false)) {
						if (!$model->irecommend_url) {
							$model->irecommend_url = $product['link'];
							$model->potential_count_reviews_3 = $product['count_reviews'];
							$model->save();
						}
						DB::update('tmp_models_3')->value('model_id', $model->id)->where('id', '=', $product['id'])->execute();
                        
                        $count_found++;
                        $found = true;
                        echo '<br /><span style="color:red;">#'.$model->id.' '.$model->name.'</span>';
                        break;        
                    }    
                } 
            }
            echo '<br /><br />';
        }
        echo '<br /><br />Всего - '.count($products).'<br />'.
            'Нашлось наших моделей - '.$count_found.'<br />';
    }
    
    public function action_test22()
    {
        $param = $this->request->param('id') ? $this->request->param('id') : null;
        if ($param) {
            list($product_id, $model_id) = explode('-', $param);
            $product = DB::select()->from('tmp_models_3')->where('id', '=', $product_id)->execute()->as_array();
            $model   = ORM::factory('model', $model_id);
            $product = $product[0];
 
            $model->irecommend_url = $product['link'];
            $model->potential_count_reviews_3 = $product['count_reviews'];
            $model->save();
            DB::update('tmp_models_3')->value('model_id', $model->id)->where('id', '=', $product['id'])->execute();   
        }
    }    
	
	// Модели с айрекомменд
    public function action_test24()
    {  
        set_time_limit(0);     
        $category_id  = 4;
        $category_url = 'http://www.irecommend.ru/category/konditsionery?tid=77028';
        
        function get_next_page_url($data) {
            if ($url = Model_ReviewParser_Abstract::_cut_str('class="pager-next"><a href="', '"', $data)) {
				$url = str_replace('&amp;', '&', $url);
                return 'http://www.irecommend.ru'.$url;
            } 
            return null;     
        }
        
        function get_model_data(&$data) {
            if ($_data = Model_ReviewParser_Abstract::_cut_str("<div class=\"block\">", "<!-- /end node.tpl.php -->", $data)) {
                return $_data;
            } 
            return null; 
        } 
        
        function get_model_link(&$data) {
            if ($link = Model_ReviewParser_Abstract::_cut_str("class=\"title\"><a href=\"", "\">", $data)) {
                return 'http://www.irecommend.ru'.$link;
            }  
            return null;   
        }
        
        function get_model_name($data) {
            if ($name = Model_ReviewParser_Abstract::_cut_str("<h2 class=\"title\">", "</h2", $data)) {
                return trim(strip_tags($name));
            } 
            return null;   
        }
        
        function get_model_image($data) {
            if ($image = Model_ReviewParser_Abstract::_cut_str(" src=\"", "\"", $data)) {
                //if ($image != 'http://i.otzovik.com/nophoto_m.png') {
                    return $image;      
                //}
            }           
            return null;   
        }
        
        function get_model_count_reviews($data) {
            if ($count = Model_ReviewParser_Abstract::_cut_str("\">Читать отзывы (", ")</a", $data)) {
                return (int)trim($count);
            } 
            return 0;   
        }

        $i = 0;
        do {
            $data = Model_ReviewParser_Abstract::_get_source_code($category_url);
            while ($model_data = get_model_data($data)) {
                $i++; 
                $name           = get_model_name($model_data);
                $link           = get_model_link($model_data);   
                $image          = get_model_image($model_data);    
                $count_reviews  = get_model_count_reviews($model_data);
                //DB::insert('tmp_models', array('category_id', 'name', 'link', 'image', 'count_reviews'))
                //    ->values(array($category_id, $name, $link, $image, $count_reviews))->execute();              
                echo  $i . ' - ' . $name . ' - ' . $link . ' - ' . $image . ' - ' . $count_reviews . '<br />';
                    
            }
            echo $category_url . '<br />';

            usleep(700000);
            //sleep(1);
            
        } while ($category_url = get_next_page_url($data));
        
    }
	
	public function action_test25()
    {
        die();
        set_time_limit(0); 
        $models = DB::select()
                ->from('models')
				->offset(100000)
                ->limit(100000)
                ->execute()
                ->as_array();
        foreach ($models as $model) {
			if ($model['market_url']) {
				$products = DB::select()
					->from('tmp_models')
					->where('reviews_link', '=', $model['market_url'])
					->execute()
					->as_array();
				if (count($products)) {
					$product = $products[0];
					DB::update('models')->value('yandexmarket_category', $product['category_id'])->where('id', '=', $model['id'])->execute();
					//continue;
				}
			}
			if ($model['otzovik_urls']) {
				$products = DB::select()
					->from('tmp_models_2')
					->where('model_id', '=', $model['id'])
					->execute()
					->as_array();
				if (count($products)) {
					$product = $products[0];
					DB::update('models')->value('otzovik_category', $product['category_id'])->where('id', '=', $model['id'])->execute();
					//continue;
				}
			}
			if ($model['irecommend_url']) {
				$products = DB::select()
					->from('tmp_models_3')
					->where('link', '=', $model['irecommend_url'])
					->execute()
					->as_array();
				if (count($products)) {
					$product = $products[0];
					DB::update('models')->value('irecommend_category', $product['category_id'])->where('id', '=', $model['id'])->execute();
					//continue;
				}
			}
        }    
        echo count($models); 
    } 
	
	public function action_test26()
    {
        die();
        set_time_limit(0); 
        $categories = DB::select()
                ->from('categories')
                ->execute()
                ->as_array();
        foreach ($categories as $category) {
			if (!$category['yandexmarket_categories'] && !$category['otzovik_categories'] &&
					!$category['irecommend_categories']) {
				continue;
			}
		
			$query = DB::select()
                ->from('models');
			
			$first = true;	
			if ($category['yandexmarket_categories']) {
				$query->where('yandexmarket_category', 'IN', explode(',', $category['yandexmarket_categories']));
				$first = false;
			}		
			if ($category['otzovik_categories']) {
				if ($first) {
					$query->where('otzovik_category', 'IN', explode(',', $category['otzovik_categories']));
				} else {
					$query->or_where('otzovik_category', 'IN', explode(',', $category['otzovik_categories']));
				}				
			}		
			if ($category['irecommend_categories']) {
				if ($first) {
					$query->where('irecommend_category', 'IN', explode(',', $category['irecommend_categories']));
				} else {
					$query->or_where('irecommend_category', 'IN', explode(',', $category['irecommend_categories']));
				}
			}
				
			$models = $query
                ->execute()
                ->as_array();
				
			foreach ($models as $model) {
				DB::update('models')->value('category_id', $category['id'])->where('id', '=', $model['id'])->execute();
			}
				
			echo count($models).'<br />';
        }    
        echo count($models); 
		
		
		//INSERT INTO shop_categories (source_id, parent_id, shop_id, name)
		//SELECT id, parent_id, 3 as shop_id, name FROM `categories_3`
    }
    
    public function action_parse_mixmarket()
    {
        die();
        Model_ProductParser_Mixmarket::instance()->parse('kras');
    }
    
    protected function _get_categories_mixmarket()
    {
        function get_childs($category, &$result) {
            $childs = DB::select()
                ->from('mixmarket_categories')
                ->where('parent_id', '=', $category['id'])
                ->execute()
                ->as_array();
            foreach ($childs as $child) {
                $result[$category['id']]['childs'][$child['id']] = $child; 
                get_childs($child, $result[$category['id']]['childs']);   
            }   
        }
        
        $result = array();
        $cat = DB::select()
                ->from('mixmarket_categories')
                ->where('id', '=', 91157)
                ->execute()
                ->as_array();
        $cat = $cat[0];
        $result[$cat['id']] = $cat;
        $result[$cat['id']]['childs'] = array();
        get_childs($cat, $result);
        
        return $result;
    }
    
    public function action_show_categories_mixmarket()
    {
        echo '<pre>';
        print_r($this->_get_categories_mixmarket());
    }
    
    public function action_categories_mixmarket()
    {
        die();
        $cats = array(
            '448' => 91157,
			'552' => 397037,
			'456' => 1001199,
			'41' => 90569,
			'39' => 278373,
			'454' => 1000602,
			'601' => 91168,
			'599' => 91163,
        );
        
        foreach ($cats as $recensee_cat => $mixmarket_cat) {
            DB::update('mixmarket_products')
                    ->value('category_id', $recensee_cat)
                    ->where('mixmarket_category_id', '=', $mixmarket_cat)
                    ->execute();    
        }
    }
    
    public function action_model_mixmarket()
    {
        die();
        set_time_limit(0); 
        $products = DB::select()
                ->from('mixmarket_products')
                ->offset(670000)
                ->limit(50000)
                ->execute()
                ->as_array();
				
		$disable_cats = array(
			91497,91503,91498,91499,418706,
			91502,1001089,91505,91504,1000395,
			91501,459013,91500,1000445,1000136,
			1000944,
			
			1000161,1000160,90625,90626,
			278370,90627,1000271,90629,
			488061,1001154,1000429,1000340,
			1000596,1000337,1000341,1000339,
			1000342,1000336,1001112,1000070,
			1001113,);
        
        $count = 0;
        $positive = 0;
        foreach ($products as $product) {
            $count++;
            echo $count.' ('.$product['id'].') - '.$product['name'].'<br />';
			
			if (in_array($product['mixmarket_category_id'], $disable_cats)) {
				continue;
			}
            
            $models = ORM::factory('model')->search($product['name'], 15);
            if ($models) {
                foreach ($models as $model) {
                    if ((strstr(Model_Main::get_clean_name($product['name']), $model->get_clean_name()) !== false ||
                            strstr($model->get_clean_name(), Model_Main::get_clean_name($product['name'])) !== false)) {
                        DB::update('mixmarket_products')
                            ->value('model_id', $model->id)
                            ->where('id', '=', $product['id'])
                            ->execute();    
                        echo '<span style="color: green">'.$model->name.'</span><br />';
                        $positive++;
                        break;       
                    } else {
                        echo '<span style="color: red">'.$model->name.'</span><br />';
                    }    
                }
            }
        }
        echo $positive;
    }
    
    public function action_price_mixmarket()
    {
        die();
        set_time_limit(0); 
        $models = ORM::factory('model')
                    ->offset(160000)
                    ->limit(20000)
                    ->find_all();
        foreach ($models as $model) {
            $data = DB::query(Database::SELECT, 
                        "SELECT COUNT(*) AS count, MIN(price) AS min_price, MAX(price) AS max_price, AVG(price) AS medium_price
                         FROM mixmarket_products
                         WHERE model_id = {$model->id}")->execute()->as_array();
            $data = $data[0];       
            $model->min_price    = $data['min_price'];
            $model->max_price    = $data['max_price'];
            $model->medium_price = $data['medium_price'];
            $model->count_prices = $data['count'];
            $model->save();
        }
        echo 'ok';
    }
    
    public function action_process_models()
    {
        die();
        /*'hostname'   => '85.25.143.75',
        'database'   => 'recensee',
        'username'   => 'recensee-loc',
        'password'   => 'rxAw8nsp4yohVOR', */
        
        if (HTTP_Request::POST == $this->request->method()) {
            $name = $this->request->post('name');
            $url  = $this->request->post('url'); 
            $model_id = $this->request->post('model_id'); 
            $model = ORM::factory('model', $model_id);
            $model->market_url = $url;
            $model->name = $name;
            $model->save();
        }      
                
        echo '<html><body>';    
        $offset = $this->request->param('id') ? : 0; 
        $models = ORM::factory('model')
            ->where('category_id', 'IN', array(396, 397, 398))
            ->and_where('market_url', 'IS', NULL)
            ->offset($offset)
            ->limit(2)
            ->find_all();
        foreach ($models as $model) {
            echo $model->id.' - '.$model->name.'<br />';
            $related = ORM::factory('model')->search($model->name, 10);
            foreach ($related as $m) {
                if ($m->id == $model->id) {
                    continue;
                }    
                echo '<small style="color: #559955;">'.$m->id.' - '.$m->name;
                if ($m->market_url) {
                    echo '  <a href="'.$m->market_url.'">+</a>';    
                }
                echo '  <a href="/index/test/copy_reviews/'.$model->id.'-'.$m->id.'" target="_blank">join</a>';                
                echo '</small><br />';
            }
            $market_url = Model_ReviewParser_YandexMarket::instance()->get_reviews_url($model->name);
            $model_name = null;
            $isset = false;
            if ($market_url) {
                $model_name = Model_ReviewParser_YandexMarket::instance()->get_model_name($market_url);
                $tmp = ORM::factory('model')->where('market_url', '=', $market_url)->find_all();
                if (count($tmp)) {
                    $isset = true;    
                }
            }
            echo '
            '.($isset ? 'есть' : 'нету').'<br />
            <form method="post">
                <input type="text" name="name" value="'.($model_name ? : '').'" style="width: 500px;" /><br />
                <input type="text" name="url" value="'.($market_url ? : '').'" style="width: 500px;" /><br />
                <input type="hidden" name="model_id" value="'.$model->id.'" />
                <input type="submit" value="Сохранить" />
            </form><br />          
            ';          
        }
        echo '</body></html>';   
        
    }
    
    public function action_copy_reviews()
    {
        die();
        $param = $this->request->param('id') ? : null;
        if ($param) {
            list($source, $dest) = explode('-', $param);
            DB::update('otzovik_reviews')
                            ->value('model_id', $dest)
                            ->where('model_id', '=', $source)
                            ->execute();
            DB::update('recensee_reviews')
                            ->value('model_id', $dest)
                            ->where('model_id', '=', $source)
                            ->execute();
            DB::update('rozetka_reviews')
                            ->value('model_id', $dest)
                            ->where('model_id', '=', $source)
                            ->execute();
            DB::update('irecommend_reviews')
                            ->value('model_id', $dest)
                            ->where('model_id', '=', $source)
                            ->execute();
            DB::update('yandexmarket_reviews')
                            ->value('model_id', $dest)
                            ->where('model_id', '=', $source)
                            ->execute();
             DB::update('mixmarket_products')
                            ->value('model_id', 0)
                            ->where('model_id', '=', $source)
                            ->execute();
            $model = ORM::factory('model', $source);
            $dest  = ORM::factory('model', $dest);
            
            if ($model->market_url) {
                $dest->market_url = $model->market_url;    
            }
            if ($model->rozetka_urls ) {
                $dest->rozetka_urls  = $model->rozetka_urls ;    
            }
            if ($model->otzovik_urls ) {
                $dest->otzovik_urls  = $model->otzovik_urls ;    
            }
            if ($model->irecommend_url ) {
                $dest->irecommend_url  = $model->irecommend_url ;    
            }
            $dest->save();
            
            $model->delete();
            echo 'ok';
        }
    }
    
    public function action_parse_ym()
    {
        die();
        set_time_limit(0);
        $models = ORM::factory('model')
            ->where('category_id', '=', 396)
            ->order_by('id', 'ASC')
            ->offset(1000)
            ->limit(200)
            ->find_all();
            
        foreach ($models as $model) {
            //http://market.yandex.ru/model-opinions.xml?modelid=2203165
            $url = str_replace('model-opinions.xml', 'model-alts.xml', $model->market_url);
            $data = Model_ReviewParser_Abstract::_get_source_code($url);
            $data = Model_ReviewParser_Abstract::_cut_str('<body>', '</body>', $data);
            
            DB::insert('tmp_cameras_3', array('id', 'competitors_html'))
                    ->values(array($model->id, $data))->execute(); 
                    
            sleep(2);
        }  
    }
    
    public function action_test30()
    {
        function extract_specs($html) {
            $specs = array();
            while ($spec = Model_ReviewParser_Abstract::_cut_str('<th class="b-properties__label b-properties__label-title"', '/td', $html)) {
                $title = Model_ReviewParser_Abstract::_cut_str('><span>', '</span>', $spec);
                $value = Model_ReviewParser_Abstract::_cut_str('<td class="b-properties__value">', '<', $spec);
                $specs[$title] = $value;      
            }
            return $specs;    
        }
        
        set_time_limit(0);
		
		$models = DB::select()
                ->from('tmp_models_3')
				->order_by('id', 'ASC')
                ->offset(0)
                ->limit(10)
                ->execute()
                ->as_array();
				
		foreach ($models as $model) {
            //$model = ORM::factory('model', $camera['id']);
            //echo $model->name.' - '.$model->market_url.'<br />';
            $specs = extract_specs($model['html']);
			
			echo '<pre>';
			print_r($specs);
            /*foreach ($specs as $title => $value) {
                if (!isset($allspecs[$title])) {
                    $allspecs[$title] = 1;    
                }    
            }    */
        }
        
		/*$cameras = DB::select()
                ->from('cameras')
                //->offset(1)
                //->limit(1)
                ->execute()
                ->as_array();
        
        $allspecs = array();       
        foreach ($cameras as $camera) {
            //$model = ORM::factory('model', $camera['id']);
            //echo $model->name.' - '.$model->market_url.'<br />';
            $specs = extract_specs($camera['source_html']);
            foreach ($specs as $title => $value) {
                if (!isset($allspecs[$title])) {
                    $allspecs[$title] = 1;    
                }    
            }    
        }
        
        echo '<pre>';
        print_r($allspecs);*/
    }
    
    public function action_test31()
    {
        function extract_specs($html) {
            $specs = array();
            while ($spec = Model_ReviewParser_Abstract::_cut_str('<th class="b-properties__label b-properties__label-title"', '/td', $html)) {
                $title = Model_ReviewParser_Abstract::_cut_str('><span>', '</span>', $spec);
                $value = Model_ReviewParser_Abstract::_cut_str('<td class="b-properties__value">', '<', $spec);
                $specs[$title] = $value;      
            }
            return $specs;    
        }
        
        $_specs = array(
            'Общее число пикселов'                 => 'total_pixels',
            'Число эффективных пикселов'         => 'effective_pixels',
            'Физический размер'                 => 'physical_size',
            'Кроп-фактор'                       => 'crop_factor',
            'Максимальное разрешение'             => 'max_resolution',
            'Тип матрицы'                         => 'matrix_type',
            'Чувствительность'                     => 'sensitivity',
            'Баланс белого'                        => 'white_balance',
            'Вспышка'                             => 'flash',
            'Стабилизатор изображения (фотосъемка)' => 'image_stabilizer',
            'Макросъёмка'                         => 'macro',
            'Скорость съемки'                     => 'shooting_speed',
            'Таймер'                             => 'timer',
            'Время работы таймера'                 => 'timer_time',
            'Формат кадра (фотосъемка)'         => 'aspect_ratio',
            'Фокусное расстояние (35 мм эквивалент)' => 'focal_length',
            'Оптический Zoom'                     => 'optical_zoom',
            'Диафрагма'                         => 'diaphragm',
            'Число оптических элементов'         => 'optical_elements_count',
            'Число групп оптических элементов'  => 'optical_element_groups_count',
            'Особенности'                         => 'features',
            'Видоискатель'                        => 'viewfinder',
            'ЖК-экран'                             => 'lcd_Screen',
            'Выдержка'                             => 'exposure',
            'Ручная настройка выдержки и диафрагмы' => 'manual_exposure',
            'Автоматическая обработка экспозиции' => 'automatic_exposure',
            'Экспокоррекция'                     => 'expocorrection',
            'Замер экспозиции'                     => 'exposure_metering',
            'Подсветка автофокуса'                 => 'af_assist',
            'Фокусировка по лицу'                 => 'face_focus',
            'Минимальное расстояние съемки'        => 'min_shooting_distance',
            'Тип карт памяти'                     => 'card_type',
            'Форматы изображения'                 => 'image_formats',
            'Интерфейсы'                         => 'interfaces',
            'Формат аккумуляторов'                 => 'batteries_format',
            'Количество аккумуляторов'             => 'batteries_count',
            'Запись видео'                         => 'video_recording',
            'Формат записи видео'                 => 'video_recording_format',
            'Максимальное разрешение роликов'     => 'max_video_resolution',
            'Максимальная частота кадров видеоролика' => 'max_video_frame_rate',
            'Максимальная частота кадров при съемке HD-видео' => 'max_hd_video_frame_rate',
            'Запись звука'                         => 'sound record',
            'Цифровой Zoom'                     => 'digital_zoom',
            'Дополнительные возможности'         => 'additional_features',
            'Дата анонсирования'                 => 'announcement_date',
            'Дата начала продаж'                 => 'release_date',
            'Размер'                             => 'size',
            'Вес'                                 => 'weight',
            'Название объектива'                 => 'lens_name',
            'Ручная фокусировка'                 => 'manual_focus',
            'Корректировка автофокуса'             => 'af_adjustment',
            'Емкость аккумулятора'                 => 'battery_capacity',
            'Разъем питания'                     => 'power_connector',
            'Видеокодеки'                         => 'video_codecs',
            'Время записи видео'                 => 'duration_video_recording',
            'Электронная стабилизация при видеосъемке' => 'video_electronic_stabilization',
            'Оптический Zoom при записи видео'     => 'video_optical_zoom',
            'Максимальная серия снимков'         => 'max_set_shots',
            'Тип ЖК-экрана'                     => 'lcd_type',
            'Объём встроенной памяти'             => 'builtin_memory',
            'Дополнительная информация'         => 'additional_info',
            'Выдержка X-Sync'                     => 'zsync_exposure',
            'Функция очистки матрицы'             => 'sensor_cleaning',
            'Поддержка сменных объективов'         => 'change_lenses',
            'Объектив в комплекте'                 => 'lens_kit',
            'Использование экрана в качестве видоискателя' => 'screen_viewfinder',
            'Поле зрения видоискателя'             => 'viewfinder_sight',
            'Брекетинг экспозиции'                 => 'exposure_bracketing',
            'Комплектация'                         => 'set',
            'Батарейный блок'                     => 'battery_pack',
            'Глубина цвета'                     => 'color_depth',
            'Второй экран'                         => 'second_screen',
            'Материал корпуса'                     => 'body_material',
            'Время включения'                     => 'switchon_time',
            'Максимальный объем карты памяти'     => 'max_builtin_memory',
            'Запись звуковых комментариев'         => 'audio_comments_record',
            'Режим серийной съемки'             => 'burst_mode',
            'Возможность установки сменных насадок' => 'change_nozzles',
            'Съемка 3D'                         => '3d_shooting',
            'Число пикселов видоискателя'         => 'viewfinder_pixels_count',
            'Тип'                                 => 'type',
            'Материал'                             => 'material',
            'Количество внутренних отделений'     => 'internal_compartments_count',
            'Внешний карман'                     => 'outside_pocket',
            'Варианты переноски'                 => 'carrying_options',
            'Снята с производства'                 => 'production_out',
            'Объём памяти в поставке'             => 'default_memory_amount',
            'ISBN'                                 => 'isbn',
            'Год издания'                         => 'year',
            'Класс устройства'                     => 'class',
            'Тип устройства'                     => 'device_type',
            'Рекомендуемая область применения'     => 'recommended_application',
            'Реальное разрешение'                 => 'real_resolution',
            'Тип лампы'                         => 'lamp_type',
            'Срок службы лампы'                 => 'lamp_life',
            'Срок службы лампы в экономичном режиме' => 'economy_lamp_life',
            'Количество ламп'                     => 'lamps_count',
            'Мощность лампы'                     => 'lamp_power',
            'Проекционное расстояние'             => 'projection_distance',
            'Размеры по диагонали'                 => 'diagonal_size',
            'Отношение расстояния к размеру изображения' => 'distance_image_size_ratio',
            'Масштабирование оптическое'         => 'optical_zooming',
            'Контрастность'                     => 'contrast',
            'Световой поток'                     => 'luminous_flux',
            'Коррекция трапецеидальных искажений' => 'trapezoidal_correction',
            'Поддерживаемые системы вещания'     => 'broadcasting_system',
            'Поддерживаемые форматы входного сигнала' => 'input_formats',
            'Входы'                             => 'inputs',
            'Выходы'                             => 'outputs',
            'Порты'                             => 'ports',
            'Размеры (ШxВxГ)'                     => 'dimensions',
            'Количество встроенных громкоговорителей' => 'builtin_speakers_count',
            'Уровень шума'                         => 'noise_level',
            'Внешние габариты (ВхТхД)'             => 'outer_dimensions',
        );
        
        die();
        set_time_limit(0);
        $cameras = DB::select()
                ->from('cameras')
                //->offset(1)
                //->limit(1)
                ->execute()
                ->as_array();
                
        /*foreach ($_specs as $spec) {
            echo 'ALTER TABLE `cameras` CHANGE `'.$spec.'` `'.$spec.'` VARCHAR( 127 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL;<br />';
        }
        die();  */
        
        $specs = array();     
        foreach ($cameras as $camera) {
            //$model = ORM::factory('model', $camera['id']);
            //echo $model->name.' - '.$model->market_url.'<br />';
            $specs = extract_specs($camera['source_html']);
            $values = array();
            foreach ($specs as $title => $value) {
                if ($_specs[$title] == 'additional_info') {
                    $values[$_specs[$title]] = $value;
                    DB::update('cameras')
                        ->set($values)
                        ->where('id', '=', $camera['id'])
                        ->execute(); 
                }       
            } 
            /*foreach ($_specs as $key => $value) {
                if ($camera[$value]) {
                    if (!isset($specs[$key])) {
                        $specs[$key] = 0;
                    }
                    $specs[$key]++;    
                }    
            }   */
        }
        asort($specs);
            echo '<pre>'; 
            print_r($specs);
    }
    
    public function action_analyze_spec()
    {
        set_time_limit(0);
        $spec = $this->request->param('id') ? : null;
        $cameras = DB::select($spec)
                ->from('tmp_cameras')
                ->where($spec, 'IS NOT', NULL)
                ->execute()
                ->as_array();
                
        echo '<pre>';
        $parts = array();
        foreach ($cameras as $_spec) {
            $_spec = $_spec[$spec];
            $tmp = explode(',', $_spec); 
            foreach ($tmp as $part) {
                $part = trim($part);
                
                /*if (strstr($part, 'фото') != false) {
                    continue;    
                }  */
                 
                if (!isset($parts[$part])) {
                    $parts[$part] = array();
                    $parts[$part]['count'] = 0;
                    $parts[$part]['sample'] = $_spec;
                }
                $parts[$part]['count']++;
            }   
        }
        echo '<pre>';
        print_r($parts);
    }
    
    public function action_test32()
    {
        die();
        set_time_limit(0);
        ORM::factory('specs_camera')->process_data();   
    }
    
    public function action_test33()
    {
        die();
        set_time_limit(0);
        $cameras = DB::select()
                ->from('tmp_cameras_2')
                ->execute()
                ->as_array();
        foreach ($cameras as $camera) {
            
            $type = Model_ReviewParser_Abstract::_cut_str('b-vlist b-vlist_type_mdash b-vlist_type_friendly"><li>', '</li', $camera['main_html']); 
            $types = Model_Main::reverse_array(Model_Specs_Camera::$_camera_types); 
            if (isset($types[$type])) {
                $spec = ORM::factory('specs_camera', $camera['id']); 
                if (!$spec->loaded()) {
                    throw new Model_Exception('Запись не загрузили');
                }
                $spec->camera_type = $types[$type];
                $spec->save(); 
            }      
        } 
    }
    
    public function action_test34()
    {
        die();
        set_time_limit(0);
        $models = DB::select()
                ->from('tmp_models_2')
                ->order_by('id', 'ASC')
                ->offset(50000)
                ->limit(10000)
                ->execute()
                ->as_array();
        foreach ($models as $model) {
            $competitors = array();
            $model['html'] = Model_ReviewParser_Abstract::_cut_str('<ul class="b-lookalike__list">', '</ul>', $model['html']);
            while ($competitor = Model_ReviewParser_Abstract::_cut_str('<a class="name" href="', '">', $model['html'])) {
                $competitor = Model_ReviewParser_Abstract::_cut_str('/model.xml?modelid=', '&amp;hid', $competitor);
                $url = 'http://market.yandex.ru/model-opinions.xml?modelid='.$competitor;
                $competitors[] = $url;                     
            }
            if (empty($competitors)) {
                echo $model['id'].'<br />'; 
                continue;    
            }
            $competitors = ORM::factory('model')
                        ->where('market_url', 'IN', $competitors)
                        ->find_all();
            if (!empty($competitors)) {
                foreach ($competitors as $competitor) {
                    try {
                        DB::insert('competitors', array('model_id', 'competitor_id'))
                            ->values(array($model['id'], $competitor->id))->execute(); 
                        //echo $model['id'].' - '.$competitor->id.'<br />';    
                    } catch (Database_Exception $e) {
                        if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
                            continue;
                        }
                        die($e->getMessage());
                    } 
                } 
                //echo $model['id'].' - '.$competitor->id.'<br />';    
            } else {
                echo $url.'<br />';
            } 
            
            /*while ($competitor = Model_ReviewParser_Abstract::_cut_str('<a class="name" href="', '">', $model['html'])) {
                $competitor = Model_ReviewParser_Abstract::_cut_str('/model.xml?modelid=', '&amp;hid', $competitor);
                $url = 'http://market.yandex.ru/model-opinions.xml?modelid='.$competitor;
                $competitor = ORM::factory('model')
                            ->where('market_url', '=', $url)
                            ->find(); 
                if ($competitor->loaded()) {
                    //DB::insert('competitors', array('model_id', 'competitor_id'))
                    //    ->values(array($model['id'], $competitor->id))->execute();     
                } else {
                    echo $url.'<br />';
                }       
            } */
                 
        } 
        echo 'ok';
    }
    
    public function action_test35()
    {
        set_time_limit(0);
        $cameras = DB::select()
                ->from('camera_specs')
                ->execute()
                ->as_array();
        foreach ($cameras as $camera) {
            $type = isset(Model_Specs_Camera::$_camera_types[$camera['camera_type']])?Model_Specs_Camera::$_camera_types[$camera['camera_type']]:'';
            echo $camera['id'].' - '.$type.'<br />';
            $competitors = DB::select()
                ->from('competitors')
                ->where('model_id', '=', $camera['id'])
                ->execute()
                ->as_array();
            $ids = array();
            foreach ($competitors as $competitor) {
                $ids[] = $competitor['competitor_id'];    
            } 
            if (count($ids)) {
                $competitors = DB::select()
                    ->from('camera_specs')
                    ->where('id', 'IN', $ids)
                    ->execute()
                    ->as_array();
                foreach ($competitors as $competitor) {
                    if ($camera['camera_type'] != $competitor['camera_type']) {
                        $type = isset(Model_Specs_Camera::$_camera_types[$competitor['camera_type']])?Model_Specs_Camera::$_camera_types[$competitor['camera_type']]:'';
                        echo '<small>'.$competitor['id'].' - '.$type.'</small><br />';    
                    }    
                } 
            }
            echo '<br /><br />';  
        }
    }
    
    public function action_collect_compareable()
    {
        die();
        set_time_limit(0);
        ORM::factory('specs_camera')->process_compare_data();   
    }
    
    public function action_test_compare()
    {
        $model1 = ORM::factory('model', 3186);
        $model2 = ORM::factory('model', 5483);
        $winner = ORM::factory('specs_camera')->get_winner($model1, $model2);
        $competitors = ORM::factory('competitor')->merge_competitors($model1, $model2);
        $data = ORM::factory('specs_camera')->compare($model1, $model2, $competitors, $winner);
        //$data = ORM::factory('specs_camera')->get_specs_full_data($model1, true);
        echo '<pre>';
        print_r($data);
        die();   
    }
    
    public function action_collect_score()
    {
        die();
        set_time_limit(0);
        ORM::factory('specs_camera')->process_score_data();   
    }
    
    public function action_test36()
    {
        die();
        set_time_limit(0);
        $models = ORM::factory('model')
            ->where('category_id', '=', 396)
            ->order_by('id', 'ASC')
            ->offset(1100)
            ->limit(100)
            ->find_all();
            
        foreach ($models as $model) {
            //http://market.yandex.ru/model-opinions.xml?modelid=2203165
            $url = str_replace('model-opinions.xml', 'model.xml', $model->market_url);
            $data = Model_ReviewParser_Abstract::_get_source_code($url);
            $data = Model_ReviewParser_Abstract::_cut_str('<body>', '</body>', $data);
            
            DB::insert('tmp_cameras_4', array('id', 'index_html'))
                    ->values(array($model->id, $data))->execute(); 
                    
            sleep(2);
        } 
        echo 'ok';  
    }
    
    public function action_test37()
    {
        die();
        set_time_limit(0);
				
		/*$models = DB::select()
                ->from('tmp_models_2')
				//->where('id', '>', 157553)
				->order_by('id', 'ASC')
                ->offset(50000)
                ->limit(10000)
                ->execute()
                ->as_array();
		$array = array();*/
		
		
		/*SELECT count(*)
FROM `tmp_models_2`
WHERE html NOT LIKE '<script%'*/
				
		/*foreach ($models as $model) {
			//if (strpos($model['html'], '<script type="text/javascript">ANCHORFREE_VER') !== 0) {
			if (strpos($model['html'], 'Регион</a>: Москва') === false) {

				$_model = ORM::factory('model', $model['id']);
				$url = str_replace('model-opinions.xml', 'model.xml', $_model->market_url);
				$data = Model_ReviewParser_Abstract::_get_source_code($url);
				
				if (!$data) {
					for($i = 0; $i < 2; $i++) {
						$data = Model_ReviewParser_Abstract::_get_source_code($url);
						if ($data) {
							break;
						}
					}
				}
				$data = Model_ReviewParser_Abstract::_cut_str('<body>', '</body>', $data);
				if (strstr($data, 'Ограничение доступа') !== false) {
					$data = null;
				}
				
				DB::update('tmp_models_2')
						->set(array('html' => $data,))
						->where('id', '=', $model['id'])
						->execute();
						
				//usleep(500000);
				sleep(2);
				echo $model['id'].'<br />';
			}
		}	

		die();		
		
		echo '<pre>';
		print_r($array);
		die();*/
				
		$models = DB::select()
                ->from('tmp_models_2')
				//->where('id', '>', 157553)
				->order_by('id', 'ASC')
                ->offset(50000)
                ->limit(10000)
                ->execute()
                ->as_array();
		$array = array();
		//echo count($models);
		foreach ($models as $model) {
			$dir = 'd:/Data/buzilio/images/';
            //$dir = APPPATH.'../media/uploads/models/';
            $dir = $dir.sprintf('%02x', $model['id'] % 0xff);
            $dir = $dir.sprintf('/%d', $model['id']);
			
			//echo $model['id'].'<br />';
			$html = $model['html'];
			
			if (!file_exists($dir.'/smain.jpg')) {
				$smain_image = null;
				$tmp = Model_ReviewParser_Abstract::_cut_str('<span class="b-model-pictures__big">', '</span>', $model['html']);
				if ($tmp) {
					$smain_image = Model_ReviewParser_Abstract::_cut_str('src="', '"', $tmp);
				}
				if (!$smain_image) {
					$tmp = Model_ReviewParser_Abstract::_cut_str('<div class="b-model-pictures">', '</div', $model['html']);
					if ($tmp) {
						$smain_image = Model_ReviewParser_Abstract::_cut_str('src="', '"', $tmp);
					}
				}
				if ($smain_image && $smain_image != '/_c/HDCjwl_qwJPr6fhBrO6Hokt7jt8.gif') {
					$data = file_get_contents($smain_image);
					$f = fopen($dir.'/smain.jpg', 'w+');
					fputs($f, $data);
					fclose($f);
				}
				$array[$model['id']] = $dir;
			}
			
			if (!file_exists($dir.'/main.jpg')) {
				$main_image = null;
				$tmp = Model_ReviewParser_Abstract::_cut_str('<span class="b-model-pictures__big">', 'target="_blank">', $html);
				if ($tmp) {
					$main_image = Model_ReviewParser_Abstract::_cut_str('href="', '"', $tmp);     
				}
				if ($main_image) {
					$data = file_get_contents($main_image);
					$f = fopen($dir.'/main.jpg', 'w+');
					fputs($f, $data);
					fclose($f);
				}
				
				$images = array();
				while ($tmp = Model_ReviewParser_Abstract::_cut_str('<span class="b-model-pictures__small">', 'target="_blank">', $model['html'])) {
					if ($image = Model_ReviewParser_Abstract::_cut_str('href="', '"', $tmp)) {
						$images[] = $image;    
					}
					
				}
				
				$n = 1;
				foreach ($images as $image) {
					$data = file_get_contents($image);
					//usleep(700000);
					$f = fopen($dir.'/'.$n.'.jpg', 'w+');
					fputs($f, $data);
					fclose($f);  
					$n++;  
				}
				//$array[$model['id']] = $dir;
			}
			
		}
		//echo count($array);
		echo '<pre>';
		print_r($array);
		
		
		die();
        /*$models = DB::select()
                ->from('tmp_models_2')
				->where('id', '>', 157553)
				->order_by('id', 'ASC')
                ->offset(0)
                ->limit(4000)
                ->execute()
                ->as_array();*/
        $i = 0;
        foreach ($models as $model) {
            //$model = ORM::factory('model', $model['id']); 
            //echo $model->market_url.'<br />';
			echo $model['id'].'<br />';
            
            $main_image = null;
            $tmp = Model_ReviewParser_Abstract::_cut_str('<span class="b-model-pictures__big">', 'target="_blank">', $model['html']);
            if ($tmp) {
                $main_image = Model_ReviewParser_Abstract::_cut_str('href="', '"', $tmp);     
            }
            
            $images = array();
            while ($tmp = Model_ReviewParser_Abstract::_cut_str('<span class="b-model-pictures__small">', 'target="_blank">', $model['html'])) {
                if ($image = Model_ReviewParser_Abstract::_cut_str('href="', '"', $tmp)) {
                    $images[] = $image;    
                }
                
            }
            
			$dir = 'd:/Data/buzilio/images/';
            //$dir = APPPATH.'../media/uploads/models/';
            $dir = $dir.sprintf('%02x', $model['id'] % 0xff);
            if (!is_dir($dir)) {
                mkdir($dir);
            }
            $dir = $dir.sprintf('/%d', $model['id']);
            if (!is_dir($dir)) {
                mkdir($dir);
            }
            
            if ($main_image) {
                $data = file_get_contents($main_image);
                $f = fopen($dir.'/main.jpg', 'w+');
                fputs($f, $data);
                fclose($f);
            }
            $n = 1;
            foreach ($images as $image) {
                $data = file_get_contents($image);
                usleep(700000);
                $f = fopen($dir.'/'.$n.'.jpg', 'w+');
                fputs($f, $data);
                fclose($f);  
                $n++;  
            }
            
            /*echo $main_image;
            echo '<pre>';
            print_r($images);
            echo '<hr />'; */
            
            /*$i++;
            if ($i > 10) break;     */
            
            
                 
        } 
        echo 'ok';
    }
    
    public function action_test38()
    {
        die();
        set_time_limit(0);
        
        $brands = array(
            '1' => 'Canon',
            '2' => 'Sony',
            '3' => 'Panasonic Lumix',
            '4' => 'Nikon',
            '5' => 'Pentax',
            '6' => 'Rekam',
            '7' => 'Samsung',
            '8' => 'Olympus',
            '9' => 'Fujifilm',
            '10' => 'Casio',
            '11' => 'Leica',
            '12' => 'Sigma',
            '13' => 'General Electric',
            '14' => 'Praktica',
            '15' => 'BenQ',
            '16' => 'Ricoh',
            '17' => 'Kodak',
            '18' => 'Rollei',
            '19' => 'Sealife',
            '20' => 'Polaroid',
            '21' => 'Hasselblad',
            '22' => 'Sanyo',
            '23' => 'Genius',
            '24' => 'Ingo',
            '25' => 'Konica',
            '26' => 'HP',
            '27' => 'Ergo',
            '28' => 'UFO',
        );
        /*foreach ($brands as $id => $brand) {
            DB::insert('brands', array('id', 'name'))
                    ->values(array($id, $brand))->execute();
        }     **/
        
        $models = ORM::factory('model')
            ->where('category_id', '=', 396)
            ->order_by('id', 'ASC')
            ->find_all();  
            
        $brands = Model_Main::reverse_array($brands); 
        foreach ($models as $model) {
            foreach ($brands as $_brand => $id) {
                if (strstr($model->name, $_brand) !== false) {
                    $_model = trim(str_replace($_brand, '', $model->name));
                    
                    $model->brand_id = $id;
                    $model->brand    = $_brand;
                    $model->model    = $_model;
                    $model->save();
                    break;   
                }   
            }   
        } 
    }
    
    public function action_test39()
    {
        set_time_limit(0);
        
        $names = array(
            'NIKON D3200',
            'NIKON D5100',

            'CANON EOS 600D',
            'NIKON D5100',

            'NIKON D7100',
            'NIKON D7000',

            'NIKON D5100',
            'NIKON D5200',

            'CANON EOS 600D',
            'CANON EOS 650D',

            'CANON EOS 600D',
            'NIKON D3200',

            'CANON EOS 650D',
            'CANON EOS 700D',

            'NIKON D3200',
            'NIKON D5200',


            'CANON EOS 650D',
            'CANON EOS 60D',

            'NIKON D600',
            'NIKON D7100',

            'CANON EOS 600D',
            'CANON EOS 60D',

            'CANON EOS 60D',
            'NIKON D7000',

            'CANON EOS 700D',
            'CANON EOS 60D',

            'CANON EOS 7D',
            'NIKON D7000',


            'CANON EOS 60D',
            'CANON EOS 7D',

            'CANON EOS 6D',
            'CANON EOS 5D Mark II',

            'CANON EOS 6D',
            'CANON EOS 7D',

            'CANON EOS 6D',
            'NIKON D600',

            'NIKON D600',
            'NIKON D800',

            'CANON EOS 5D Mark III',
            'CANON EOS 6D',


            'CANON PowerShot SX50 HS',
            'SONY CYBER-SHOT DSC-HX300',

            'CANON PowerShot SX50 HS',
            'NIKON COOLPIX P520',

            'NIKON COOLPIX P520',
            'NIKON COOLPIX P510',

            'CANON PowerShot SX50 HS',
            'NIKON COOLPIX P510',


            'SONY NEX-5N',
            'SONY NEX-5R',

            'SONY ALPHA NEX-6',
            'SONY NEX-7',

            'SONY ALPHA NEX-6',
            'SONY NEX-5R',

            'SONY ALPHA NEX-3N',
            'SONY NEX-F3',


            'CANON POWERSHOT S110',
            'CANON POWERSHOT S100',

            'PANASONIC LUMIX DMC-LX7',
            'SONY CYBERSHOT DSC-RX100',

            'CANON POWERSHOT S110',
            'SONY CYBERSHOT DSC-RX100',

            'FUJIFILM FINEPIX x20',
            'SONY CYBERSHOT DSC-RX100',


            'SONY CYBER-SHOT DSC-TX20',
            'SONY CYBER-SHOT DSC-TX30',

            'CANON POWERSHOT IXUS 500 HS',
            'CANON POWERSHOT IXUS 510 HS',

            'SONY CYBER-SHOT DSC-WX70',
            'SONY CYBER-SHOT WX50',

            'SONY CYBER-SHOT DSC-TX20',
            'SONY CYBERSHOT TX10',
        );
        foreach ($names as $name) {
            $models = ORM::factory('model')->search($name, 5);
            echo $name.'<br />';
            foreach ($models as $model) {
                $specs = ORM::factory('specs_camera', $model->id);
                echo $model->id.' - '.$model->name.' - '.$specs->camera_type.'<br />';   
            }
            echo '<br /></br />';
            
        }
        
        
    }
    
    public function action_test40()
    {
        die();
        set_time_limit(0);
        $models = ORM::factory('model')
            ->where('category_id', '=', 396)
            ->and_where('id', '>', 54616)
            ->order_by('id', 'ASC')
            ->offset(400)
            ->limit(100)
            ->find_all();
            
        foreach ($models as $model) {
            //http://market.yandex.ru/model-opinions.xml?modelid=2203165
            $url = str_replace('model-opinions.xml', 'offers.xml', $model->market_url);
            $page = 1;
            while ($url) {
                //echo $url.'<br />';
                $data = Model_ReviewParser_Abstract::_get_source_code($url);
                $data = Model_ReviewParser_Abstract::_cut_str('<body>', '</body>', $data);
                $url  = Model_ReviewParser_Abstract::_cut_str('class="b-pager__next" href="', '"', $data);
                if ($url) {
                    $url = 'http://market.yandex.ru/'.$url;
                }
                
                DB::insert('tmp_cameras_5', array('id', 'page', 'index_html'))
                        ->values(array($model->id, $page, $data))->execute(); 
                $page++;        
                sleep(2);   
            }
        } 
        echo count($models).' ok';  
    }
	
	public function action_test41()
    {
		die();
        set_time_limit(0); 
        $models = ORM::factory('model')
            //->where('category_id', '=', 396)
            ->order_by('id', 'ASC')
            ->offset(50000)
            ->limit(10000)
            ->find_all();
        foreach ($models as $model) {
            $data = DB::query(Database::SELECT, 
                        "SELECT COUNT(*) AS count, MIN(price) AS min_price, MAX(price) AS max_price, AVG(price) AS medium_price
                         FROM prices
                         WHERE model_id = {$model->id}")->execute()->as_array();
            $data = $data[0];       
            $model->min_price    = $data['min_price'];
            $model->max_price    = $data['max_price'];
            $model->medium_price = $data['medium_price'];
            $model->count_prices = $data['count'];
            $model->save();
        }
        echo 'ok';
		
		/*UPDATE models SET
		min_price = (SELECT MIN(price) FROM prices WHERE model_id = models.id),
		max_price = (SELECT MAX(price) FROM prices WHERE model_id = models.id),
		medium_price = (SELECT AVG(price) FROM prices WHERE model_id = models.id),
		count_prices = (SELECT COUNT(*) FROM prices WHERE model_id = models.id)*/
	
        /*die();
        set_time_limit(0);
        $models = ORM::factory('model')
            ->where('category_id', '=', 396)
            ->order_by('id', 'ASC')
            ->offset(1200)
            ->limit(200)
            ->find_all();
            
        foreach ($models as $model) {
			//echo $model->market_url.'<br />';
            $pages = DB::select()
                    ->from('tmp_cameras_5')
                    ->where('id', '=', $model->id)
					->and_where('page', '=', 1)
                    ->execute()
                    ->as_array();
					
			$i = 0;
			foreach ($pages as $page) {
				while ($block = Model_ReviewParser_Abstract::_cut_str('<div class="b-offers  b-offers__offers"', '<div class="b-offers__feats">', $page['index_html'])) {
					//echo $block;
					//die();
					
					$link  = Model_ReviewParser_Abstract::_cut_str('<a href="', '"', $block);
					$name  = strip_tags(Model_ReviewParser_Abstract::_cut_str('<h3 class="b-offers__title">', '</h3>', $block));
					$image = Model_ReviewParser_Abstract::_cut_str('b-offers__img" src="', '"', $block);
					$price = preg_replace('/[^0-9]/i', '', Model_ReviewParser_Abstract::_cut_str('<span class="b-prices__num">', '</span>', $block));
					$shop  = Model_ReviewParser_Abstract::_cut_str('class="shop-link black"', '/a><span', $block);
					if ($shop) {
						$shop = Model_ReviewParser_Abstract::_cut_str('">', '<', $shop);
					}
					$description = '';
					while ($_description = strip_tags(Model_ReviewParser_Abstract::_cut_str('<p class="b-offers__spec">', '</p>', $block))) {
						$description = trim(str_replace('ещёскрыть', '', $_description));
					}
					
					DB::insert('prices', array('model_id', 'link', 'name', 'image', 'price', 'shop', 'description'))
                        ->values(array($model->id, $link, $name, $image, $price, $shop, $description))->execute();
					echo $link.'<br />';
					echo $name.'<br />';
					echo $image.'<br />';
					echo $price.'<br />';
					echo $shop.'<br />';
					echo $description.'<br /><hr />';
					$i++;
				}
			}
        } 
        echo count($models).' ok'; */ 
    }
    
    public function action_test42()
    {
        //die();
        set_time_limit(0);
        
        /*$brands = array(
            '1' => 'Canon',
            '2' => 'Sony',
            '3' => 'Panasonic Lumix',
            '4' => 'Nikon',
            '5' => 'Pentax',
            '6' => 'Rekam',
            '7' => 'Samsung',
            '8' => 'Olympus',
            '9' => 'Fujifilm',
            '10' => 'Casio',
            '11' => 'Leica',
            '12' => 'Sigma',
            '13' => 'General Electric',
            '14' => 'Praktica',
            '15' => 'BenQ',
            '16' => 'Ricoh',
            '17' => 'Kodak',
            '18' => 'Rollei',
            '19' => 'Sealife',
            '20' => 'Polaroid',
            '21' => 'Hasselblad',
            '22' => 'Sanyo',
            '23' => 'Genius',
            '24' => 'Ingo',
            '25' => 'Konica',
            '26' => 'HP',
            '27' => 'Ergo',
            '28' => 'UFO',
        );    */
        
        $models = DB::select()
                ->from('models_1')
                //->offset(1100)
                //->limit(100)
                ->execute()
                ->as_array();
                
        $brands = array();
        foreach ($models as $model) {
            $tmp = explode(' ', $model['name']);
			$original = $tmp[0];
            $brand = ucfirst(mb_strtolower($tmp[0]));
            if (!isset($brands[$brand])) {
                $brands[$brand]['count'] = 0;
				$brands[$brand]['original'] = $original;
                $brands[$brand]['samples'] = array();
            }
            $brands[$brand]['count']++;
            $brands[$brand]['samples'][] = $model['name'];
        }
        
		echo '<pre>';
		$i = 1;
        foreach ($brands as $_key => &$brand) {
            /*if ($brand['count'] == 1) {
                unset($brands[$key]);
            }*/
			$twowords = true;
			$brand['tmp2'] = array();
			foreach ($brand['samples'] as $name) {
				$tmp = explode(' ', $name);
				$tmp2 = $tmp[1];
				$brand['tmp2'][] = $tmp2;
			}
			foreach ($brand['tmp2'] as $key => $tmp2) {
				if ($key == 0) {
					continue;
				}
				if ($brand['tmp2'][$key-1] != $brand['tmp2'][$key]) {
					$twowords = false;
				}
			}
			if ($brand['count'] == 1) {
				$twowords = true;
			}
			
			$tmp3 = $_key;
			if ($_key != $brand['original']) {
				//$tmp3 .= ' - '.$brand['original'];
				$tmp3 = $brand['original'];
			}
			echo "'{$i}' => '{$tmp3}',<br />";
			if ($twowords) {
				print_r($brand['samples']);
			}
			/*if ($brand['count'] == 1) {
				echo "'{$i}' => '{$brand['original']} - # {$brand['samples'][0]} #',<br />";
			}*/
			$i++;
        }
        
		echo '-------------------<br />';
        //echo count($brands);
        
        echo '<pre>';
        //print_r($brands);
        
        /*foreach ($brands as $id => $brand) {
            DB::insert('brands', array('id', 'name'))
                    ->values(array($id, $brand))->execute();
        }     **/
        
        /*$models = ORM::factory('model')
            ->where('category_id', '=', 396)
            ->order_by('id', 'ASC')
            ->find_all();  
            
        $brands = Model_Main::reverse_array($brands); 
        foreach ($models as $model) {
            foreach ($brands as $_brand => $id) {
                if (strstr($model->name, $_brand) !== false) {
                    $_model = trim(str_replace($_brand, '', $model->name));
                    
                    $model->brand_id = $id;
                    $model->brand    = $_brand;
                    $model->model    = $_model;
                    $model->save();
                    break;   
                }   
            }   
        }   */
    }
	
	public function action_test43()
	{
		
		
		set_time_limit(0);
		die();
		
		/*foreach ($brands as $id => $brand) {
			$brand = str_replace("'", "\'", $brand);
            DB::insert('brands', array('id', 'name'))
                    ->values(array($id, $brand))->execute();
        }
		
		echo 'ok';
		die();*/
		
		$tmp = array();
		foreach ($brands as $id => $brand) {
			if (!isset($tmp[mb_strlen($brand)])) {
				$tmp[mb_strlen($brand)] = array();
			}
			$tmp[mb_strlen($brand)][$id] = $brand;
		}		
		krsort($tmp);
		
		$brands = array();
		foreach ($tmp as $len => $_brands) {
			//$brands = array_merge($brands, $_brands);
			foreach ($_brands as $id => $name) {
				$brands[$id] = $name;
			}
		}
		
		$models = DB::select()
                ->from('models_1')
                ->order_by('id', 'ASC')
				->offset(55000)
				->limit(10000)
                ->execute()
                ->as_array();
           
		//var_dump(setlocale(LC_ALL, "russian","ru_RU.CP1251")); 
        //$brands = Model_Main::reverse_array($brands); 
        foreach ($models as $model) {
			$name = mb_strtolower($model['name']);			
			echo $model['name'].'<br />';
            foreach ($brands as $id => $_brand) {
				$tmp_brand = mb_strtolower($_brand);
                if (strpos($name, $tmp_brand) === 0) {
					$tmp_brand = str_replace(array('&', "'", '"', "/"), array('\&', "\'", '\"', "\/"), $tmp_brand); 
					$_model = trim(preg_replace('/^'.$tmp_brand.'/ui', '', $model['name']));
					$name = $_brand.' '.$_model;
					//echo '---'.$tmp_brand.'<br />';
					echo '---'.$_brand.'<br />';
					echo '---'.$_model.'<br />';
					echo '---'.$name.'<br />';
                    
					//$_brand = str_replace("'", "\'", $_brand);
					DB::update('models_1')
						->set(array('brand_id' => $id, 'brand' => $_brand, 'model' => $_model, 'name' => $name,))
						->where('id', '=', $model['id'])
						->execute();      
                    break;   
                }   
            }   
        } 
		echo 'ok';
		
	}
    
	public function action_test44()
    {
        die();
        set_time_limit(0);
		
		//var_dump(Model_ReviewParser_Abstract::_get_source_code('http://market.yandex.ru/'));
		//die();
			
		$models = DB::select()
                ->from('models_1')
				->where('id', '>', 1330)
                ->order_by('id', 'ASC')
				->offset(0)
				->limit(10000)
                ->execute()
                ->as_array();
				
		/*$models = DB::query(Database::SELECT,  'SELECT *
												FROM `models_1`
												WHERE NOT
												EXISTS (

												SELECT id
												FROM tmp_models_2
												WHERE id = models_1.id
												)')->execute()->as_array();*/
            
        foreach ($models as $model) {
            //http://market.yandex.ru/model-opinions.xml?modelid=2203165
            $url = str_replace('model-opinions.xml', 'model-spec.xml', $model['market_url']);
            $data = Model_ReviewParser_Abstract::_get_source_code($url);
			
			if (!$data) {
				for($i = 0; $i < 2; $i++) {
					$data = Model_ReviewParser_Abstract::_get_source_code($url);
					if ($data) {
						break;
					}
				}
			}
            $data = Model_ReviewParser_Abstract::_cut_str('<body>', '</body>', $data);
            
            DB::insert('tmp_models_3', array('id', 'html'))
                    ->values(array($model['id'], $data))->execute(); 
                    
            //usleep(500000);
			sleep(1);
        } 
        echo 'ok';  
    }
	
	public function action_test45()
    {
        die();
        set_time_limit(0);
		
		function extract_full_specs($html) {
            $specs = array();
			$html = Model_ReviewParser_Abstract::_cut_str('<table class="b-properties">', '</table>', $html);
			
			$pointer = null;
			while ($row = Model_ReviewParser_Abstract::_cut_str('<tr>', '</tr>', $html)) {
				if (strstr($row, 'class="b-properties__title"') !== false) {
					$title = Model_ReviewParser_Abstract::_cut_str(' class="b-properties__title">', '</th>', $row);
					$specs[$title] = array();
					$pointer = &$specs[$title];
				} else {
					$title = Model_ReviewParser_Abstract::_cut_str('><span>', '</span>', $row);
					$value = Model_ReviewParser_Abstract::_cut_str('<td class="b-properties__value">', '<', $row);
					$pointer[$title] = $value;
				}
			}
			
            return $specs;    
        }
		
		/*$model = ORM::factory('model', 3);
		foreach (json_decode($model->specs) as $group => $data) {
			echo $group.'<br />';
			foreach ($data as $title => $value) {
				echo '-'.$title.':'.$value.'<br />';
			}
		}
		die();*/
			
		$models = DB::select()
                ->from('tmp_models_3')
				//->where('id', '>', 157553)
                ->order_by('id', 'ASC')
				->offset(50000)
				->limit(10000)
                ->execute()
                ->as_array();
          
		$n = 0;
        foreach ($models as $model) {
            $specs = extract_full_specs($model['html']);
			
			if (empty($specs)) {
				$n++;
				DB::update('models')
					->set(array('specs' => null,))
					->where('id', '=', $model['id'])
					->execute();
			}
			/*$specs = json_encode($specs);
            
            DB::update('models')
				->set(array('specs' => $specs,))
				->where('id', '=', $model['id'])
				->execute();*/
        }
		echo $n;		
        echo 'ok';  
    }
	
	public function action_test46()
	{
		die();
        set_time_limit(0);
	
		$models = DB::select()
                ->from('tmp_models_2')
                ->order_by('id', 'ASC')
				->offset(50000)
				->limit(10000)
                ->execute()
                ->as_array();
	
		foreach ($models as $model) {
			while ($block = Model_ReviewParser_Abstract::_cut_str('class="b-top5-offers__line_', '</tr>', $model['html'])) {
				$shop  = Model_ReviewParser_Abstract::_cut_str('class="b-top5-offers__shop">', '</td>', $block);
				if ($shop) {
					$shop = Model_ReviewParser_Abstract::_cut_str('">', '<', $shop);
				}
				
				$delivery = Model_ReviewParser_Abstract::_cut_str('class="b-top5-offers__list__delivery">', '</td>', $block);
				if ($delivery) {
					$delivery = trim($delivery);
				}
			
				$link  = Model_ReviewParser_Abstract::_cut_str('class="b-top5-offers__list__button">', '</td>', $block);
				if ($link) {
					$link = Model_ReviewParser_Abstract::_cut_str('href="', '"', $link);
				}

				$price  = Model_ReviewParser_Abstract::_cut_str('class="b-top5-offers__list__price">', '</td>', $block);
				if ($price) {
					$price = preg_replace('/[^0-9]/i', '', Model_ReviewParser_Abstract::_cut_str('<span class="b-prices__num">', '</span>', $price));
				}
				
				DB::insert('prices', array('model_id', 'link', 'price', 'shop', 'delivery'))
					->values(array($model['id'], $link, $price, $shop, $delivery))->execute();
				/*echo $link.'<br />';
				echo $price.'<br />';
				echo $delivery.'<br />';
				echo $shop.'<br /><hr />';*/
			}
		}
		echo 'ok';
	}
    
    public function action_test47()
    {
        die();
        set_time_limit(0);
        
        $categories = ORM::factory('category')
                        ->limit(10)
                        ->find_all();
        
        $specs = array();                
        foreach ($categories as $category) {
            $specs[$category->url] = array();
            $models = ORM::factory('model')
                            ->where('category_id', '=', $category->id)
                            ->find_all();
            
            $n = 1;
            foreach ($models as $model) {
                if (!$model->specs) {
                    continue;
                }    
                foreach (json_decode($model->specs) as $group => $items) {
                    foreach ($items as $spec => $value) {
                        if (!isset($specs[$category->url][$spec])) {
                            $specs[$category->url][$spec] = array(
                                'key' => 'spec_'.$n,
                                'count' => 0,
                                'sample' => $value
                            ); 
                            $n++;   
                        }        
                        $specs[$category->url][$spec]['count']++;                          
                    }    
                }
            }                
                
        }
        
        echo '<pre>';
        print_r($specs);
        
        
        /*$spec = $this->request->param('id') ? : null;
        $cameras = DB::select($spec)
                ->from('tmp_cameras')
                ->where($spec, 'IS NOT', NULL)
                ->execute()
                ->as_array();
                
        echo '<pre>';
        $parts = array();
        foreach ($cameras as $_spec) {
            $_spec = $_spec[$spec];
            $tmp = explode(',', $_spec); 
            foreach ($tmp as $part) {
                $part = trim($part);
                
                if (strstr($part, 'фото') != false) {
                    continue;    
                }  
                 
                if (!isset($parts[$part])) {
                    $parts[$part] = array();
                    $parts[$part]['count'] = 0;
                    $parts[$part]['sample'] = $_spec;
                }
                $parts[$part]['count']++;
            }   
        }
        echo '<pre>';
        print_r($parts);    */
    }
    
    public function action_test48()
    {
        die();
        set_time_limit(0);
        
        $categories = ORM::factory('category')
                        //->limit(10)
                        ->find_all();
        
        $specs = array();                
        foreach ($categories as $category) {
            $minmax = DB::select(array(DB::expr('MIN(reviews_score)'), 'min'), array(DB::expr('MAX(reviews_score)'), 'max'))
                        ->from('models')
                        ->where('category_id', '=', $category->id)
                        ->execute()
                        ->as_array();
            $minmax = $minmax[0];
                        
            //echo $minmax['min'].' - '.$minmax['max'].'<br />';
                              
            $models = ORM::factory('model')
                            ->where('category_id', '=', $category->id)
                            ->find_all();
            foreach ($models as $model) {
                $normalized_reviews_score = ($model->reviews_score - $minmax['min'])/($minmax['max'] - $minmax['min']) * 100; 
                $normalized_reviews_score = (int)$normalized_reviews_score;
                if ($normalized_reviews_score != 0) {
                    $normalized_reviews_score -= 1;    
                }
                
                $model->normalized_reviews_score = $normalized_reviews_score;
                $model->save();
                //echo $normalized_reviews_score.'<br />';  
            }
                
        }
    }
} 
